const connection = require('../app/database')

class UserService {
  async create(user){
    const {name,password} = user
    const statement = `INSERT INTO user (name,password) VALUES (?,?)`
    const result = await connection.execute(statement,[name,password])
    return result
  }

  async getUser(name){
    const statement = `SELECT * FROM user WHERE name=?`
    const result = await connection.execute(statement,[name]);
    return result[0]
  }

  async getAvatarInfo(userId){
    const statement = `SELECT * FROM avatar WHERE user_id = ?`
    const [result] = await connection.execute(statement,[userId]);
    return result.pop()
  }

  async updateAvatarUrl(avatarUrl,userId){
    const statement = `
      UPDATE user SET avatar_url = ? WHERE id = ?
    `
    const [result] = await connection.execute(statement,[avatarUrl,userId])
    return result
  }
}
module.exports = new UserService();